package com.findviewbydream.evil.eda.Activity;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;

import com.findviewbydream.evil.eda.R;

/********************************************
 * 工程名：Eda
 * 包名：com.findviewbydream.evil.eda.Activity
 * 时间：16-7-27 上午8:05
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：带有toolbar的基础类
 ********************************************/
public abstract class BaseActivity extends AppCompatActivity
{
    @LayoutRes
    public abstract int setContentView();

    public abstract String setToolbarTitle();

    public abstract void onCreate();

    protected Bundle savedInstanceState;

    private Toolbar toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        this.savedInstanceState = savedInstanceState;
        super.onCreate(savedInstanceState);
        setContentView(setContentView());
        initView();
        setToolbar(setToolbarTitle());
        onCreate();
    }


    /**
     * 初始化控件
     */
    private void initView()
    {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
    }

    /**
     * 设置toolbar
     */
    private void setToolbar(String title)
    {
        try
        {
            toolbar.setTitle(title);
            setSupportActionBar(toolbar);
            if (getSupportActionBar() != null)
            {
                getSupportActionBar().setHomeButtonEnabled(true);
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }
        }
        catch (Exception e)
        {
            Log.e("错误", "布局中不存在Toolbar");
        }
    }

    /**
     * toolbar返回键监听
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if (item.getItemId() == android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
























